<include file="Public:js" />
<style>
    input[type=checkbox]{
        -webkit-appearance: none;
        -webkit-box-shadow:none;
        background:  url(__PUBLIC__/img/shoppingmarket_quanxuan@2x.png) no-repeat;
        height: 18px;
        vertical-align: middle;
        width: 18px;
        background-size: 100% 100%;
        -webkit-border: none;
        hidefocus;
    }
    input[type=checkbox]:checked{
        background:  url(__PUBLIC__/img/shoppingmarket_gou@2x.png) no-repeat;
        -webkit-box-shadow:none;
        height: 18px;
        vertical-align: middle;
        width: 18px;
        background-size: 100% 100%;
        -webkit-border: none;
    }
    .gw_num{border: 1px solid #dbdbdb;width: 106px;height:26px;overflow: hidden;margin-top:5px;}
    .gw_num em{display: block;height: 26px;width: 26px;float: left;color: #7A7979;border-right: 1px solid #dbdbdb;text-align: center;cursor: pointer;}
    .gw_num .num{display: block !important;float: left;text-align: center;width: 45px !important;font-style: normal;font-size: 14px !important;height:26px !important; line-height: 24px;border: 0 !important;background: #FFF}
    .gw_num em.add{float: right;border-right: 0;border-left: 1px solid #dbdbdb;background: #F1F1F1}
    .gw_num em.jian{background: #F1F1F1}
</style>
<div class="page-group">
    <div class="page" data-page="book_kind">
        <header class="bar bar-nav">
            <a onclick="window.location.href='{:U("User/index")}'" class="icon" style="width: 40%">
            <img src="__PUBLIC__/img/iconfont_back@2x.png" class="back_img">
            </a>
        </header>
        <div class="content doublecontent"   id="content" style="background: #FFF">
            <div class="shopping_cart_kind" style="border: none" >
                我要借阅的图书
            </div>
            <div class="shopping_cart_inc" style="background: #FFF">
                <div class="list-block media-list">
                    <ul>
                        <volist name="d" id="v">
                            <li style="margin-top: 5px">
                                <div style="position: absolute;top:1.2rem;bottom:15px;left:10px">
                                    <input class="cbox" type="checkbox" name="goods_check" value="{$v.quantity}" data-id="{$v.book_id}" onclick="singlecheck()" id="goods_check{$v.book_id}">
                                </div>
                                <div style="position: absolute;top:1.2rem;bottom:15px;right:15px;z-index:10;"><img onclick="cartdelete(1,{$v.book_id})" src="__PUBLIC__/img/delete@2x.png" style="width: 18px;height: 18px"></div>
                                <a  class="item-link item-content" style="background: #F5F5F5">
                                    <div class="item-media">
                                        <img src="{$v.b_logo}" style='width: 4rem;max-width:4rem;max-height: 4rem;z-index:19' onclick="window.location.href='{:U('Book/detail','id='.$v['book_id'])}'" />
                                    </div>
                                    <div class="item-inner" style="background-image: none;">
                                        <div class="item-title-row">
                                            <div class="item-title">{$v.sm}</div>
                                        </div>
                                        <div class="gw_num">
                                            <em class="jian"  id="jian{$v.book_id}" onclick="jian({$v.book_id})">-</em>
                                            <input  type="text" value="{$v.quantity}" class="num"  id="num{$v.book_id}" readonly="readonly"/>
                                            <em class="add"  id="add{$v.book_id}" onclick="add({$v.book_id})">+</em>
                                        </div>
                                    </div>
                                </a>
                            </li>
                        </volist>

                    </ul>
                </div>
            </div>
            <div style="padding: 10px;background: #FFF">
                <img src="__PUBLIC__/img/attention@2x.png">&nbsp;&nbsp;<font color="#F3A490">单次可借{:C("rentmax_wx")/2}本，至多可借{:C("rentmax_wx")}本</font>
            </div>
        </div>
        <nav class="bar bar-tab old_bar">
            <a class="tab-item external">
                <input type="checkbox" name="checkall" class="cbox">&nbsp;&nbsp;<span class="label_all_check">全选</span>
            </a>
            <a class="tab-item external" href="#">
                <span class="label_sum_cost"><font class="label_sum_cost_fee">共<span id="sum_money">0</span>本</font></span>
            </a>
            <a class="tab-item external" >
                <input type="button" value="我要借阅(0)" class="goto_check alert-text"  id="goto_check"  style="border-radius:0px;"  onclick="check_over_four();">
            </a>
        </nav>

        <form id="myform" action="{:U('User/bag_order_confirm')}" method="get">
            <input type="hidden" name="liststr" id="liststr" value=""/>
        </form>
    </div>
</div>
<script>
    var $rentmax={:C('rentmax_wx') /2};
    function add(i){
        var t = $("#num"+i);
        t.val(parseFloat(t.val()) + 1);
        $("#goods_check"+i).val(parseFloat(t.val()));
        whoIsChecked();
    }

    function jian(i){
        var t = $("#num"+i);
        if(parseFloat(t.val())==0){
            return;
        }
        t.val(parseFloat(t.val()) - 1);
        $("#goods_check"+i).val(parseFloat(t.val()));
        whoIsChecked();
    }
    //最后的结算检查
    function  check_over_four(){
        var length_rent=parseInt($("#sum_money").html());
        if(length_rent>$rentmax){
            //提交结算  借的书
            alert('您至多借'+$rentmax+'本外借');
            return
        }else if(length_rent==0){
            alert('至少借一本');
            return
        }else{
            //4本以下都可借
            var liststr="";
            var obj = document.getElementsByName("goods_check");
            var check_val = [];
            for(var k in obj){
                if(obj[k].checked)
                    check_val.push(obj[k].getAttribute("data-id")+"-"+obj[k].value);
            }
            for (var a = 0; a < check_val.length; a++) {
                liststr=liststr+$.trim(check_val[a])+",";
            }
            $("#liststr").val(liststr);
            //下单前检查
            $.post('{:U("Order/rentCheckVip")}',{is_ffree:0,liststr:liststr},function(data){
                if(data.status=="success"){
                    $("#myform").submit();
                }else{
                    if(data.code=='103'){
                        alert(data.data);
                        //会员期限,前往充值
                        window.location.href='{:U("User/vip_charge")}';
                    }else{
                        alert(data.data);
                    }
                }
            },"json");
        }
    }

    //检查当前选中图书，每次有操作都调用来调整借阅数量
    function whoIsChecked() {
        var sum_solo=0;
        var obj = document.getElementsByName("goods_check");
        var check_val = [];
        for(var k in obj){
            if(obj[k].checked)
                check_val.push(obj[k].value);
        }
        for (var a = 0; a < check_val.length; a++) {
            sum_solo=sum_solo+parseInt($.trim(check_val[a]));
        }
        $("#goto_check").val("我要借阅("+sum_solo+")");
        $("#sum_money").html(sum_solo);
    }
    /*
     var sum=0;
     $.each($(".num"),function(index,item){
     //如果数字有小数点，就使用 parsetFloat()
     sum+= parseInt($.trim($(item).val()));
     });
     */
    function singlecheck(){
        if($(this).is(':checked')){
            $(this).attr("checked","checked");
        }else{
            $(this).attr("checked","");
        }
        whoIsChecked();
    }
    $("input[name=checkall]").click(function () {
        var obj = $('input[name="goods_check"]');
        if ($(this).is(':checked')) {
            obj.each(function(){
                this.checked=true;
            });
        }else {
            obj.each(function(){
                this.checked=false;
            });
        }
        whoIsChecked();
    });

    function cartdelete(type,id){
        if(confirm("是否删除此书")){
            $.post("{:U('Cart/deletebook')}",{id:id,type:type},function(data){
                window.location.reload();
            });
        }
    }
</script>